package com.wuxiaolong.tools.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuxiaolong.tools.entity.UserEntity;
import com.wuxiaolong.tools.mapper.UserEntityMapper;
import com.wuxiaolong.tools.model.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Description:
 *
 * @author Leo
 * @date 2020-04-12
 */
@Slf4j
@Controller
@RequestMapping(value = "/test")
public class TestController {

    @Autowired
    private UserEntityMapper userEntityMapper;

    /**
     * 测试分页
     * @param httpHeaders
     * @param req
     * @param pageNum
     * @param pageSize
     * @param userEntityParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/page/{pageNum}/{pageSize}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public JsonResult<PageInfo<UserEntity>> pay(@RequestHeader HttpHeaders httpHeaders, HttpServletRequest req,
                                                @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize,
                                                @RequestBody UserEntity userEntityParam) {

        log.info("请求参数controller：{}", JSON.toJSONString(userEntityParam));

        PageHelper.startPage(pageNum, pageSize, true);
        List<UserEntity> list = userEntityMapper.selectListByDynamicConditionsAndPage(userEntityParam);
        PageInfo<UserEntity> pageInfo = new PageInfo<>(list);
        return new JsonResult<PageInfo<UserEntity>>(pageInfo);
    }


    /**
     * 测试Thymeleaf
     * 要求使用@Controller
     * @return
     */
    @GetMapping("/page")
    public String testThymeleaf(){
        return "/pages/test";
    }
}
